var crypto = require('crypto');
var jwt = require('jsonwebtoken');

import $ from '../utils';


//md5值加密
module.exports.md5 = function (value) {
    return new Promise((resolve)=>{
        let md5 = crypto.createHash('md5');
        let result = md5.update(value).digest('hex');
        resolve(result)
    })
}
//密码加密
module.exports.cryptPwd = function (pwd) {
    return new Promise((resolve)=>{
        let saltPwd = pwd + ':' + $.config.Salt;
        let md5 = crypto.createHash('md5');
        let result = md5.update(saltPwd).digest('hex');
            resolve(result)
    })
}
//创建UserId
module.exports.cryptUserId = function (name) {
    return new Promise((resolve)=>{
        let saltName = name + ':' + $.config.Salt;
        let md5 = crypto.createHash('md5');
        let result = md5.update(saltName).digest('hex');
        resolve(result)
    })
}
//生成Token
module.exports.createToken = function (json) {
    return jwt.sign(json,$.config.TokenSecret,{'expiresIn':10})
}

//验证Token是否有效
module.exports.authSession = async function (req, res, next){
    let token = req.session.token;
    //console.log(token);
    if($.base.isEmpty(token)){return res.send(JSON.stringify({"status":'-9999',"msg":'用户登录失效,请重新登录',"dataset":[]}))}

    next();
    // return;
}

//

